/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.pg.misra.rules;

import java.util.ArrayList;
import java.util.List;
import pl.pg.misra.AST.AST;
import pl.pg.misra.Interfaces.IRule;

/**
 *
 * @author Paweł
 */
public class NoFloatingPointComparisonRule extends AbstractRule implements IRule{

    @Override
    public List<String> checkRule(List<AST> asts) {
        List<String> result = new ArrayList<>();
        List<String> parameters = new ArrayList<>();
        
        parameters.add("==");
        parameters.add("<=");
        parameters.add(">=");
        
        for (AST node : asts){
            List<AST> searchResult = new ArrayList<>();
            
            searchResult = findNodesByTypeAndParameters("BinaryOperator", node, searchResult, parameters);
            
            for (AST operator : searchResult){
                List<AST> searchEvaluation = findFloatingReferences(operator);
                
                if (searchEvaluation.size() > 0){
                    result.add(buildViolationMessage(node, "13.3", "comparison of floating point values"));
                }
            }
        }
        
        return result;
    }
    
    
    private List<AST> findFloatingReferences(AST node){
        List<AST> result = new ArrayList<>();
        
        for (AST child : node.getChildrens()){
            if (child.getNodeType().equals("ImplicitCastExpr") || child.getNodeType().equals("FloatingLiteral")){
                if (child.getAttributes().contains("float") || child.getAttributes().contains("double") ){
                    result.add(child);
                }
            }
        }
        
        return result;
    }
}
